
import * as appConfig from '../../../app-config.js'
var app = getApp()
Page({
  data: {
    detailPicUrls:['../../../image/addPic.png'],
    orderId:'',
    name:'',
    phone:'',
    remarks:''
  },
  onLoad: function(options) {
    this.setData({
      orderId:options.orderId
    })
  },
  //上传商品詳情圖片
  bindUploadImg(){  
    let that = this
    wx.chooseImage({
      count:5,
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        let detailPicUrls = []
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url:appConfig.apiBase+'wx_app/image/upload',
            filePath:tempFilePaths[i],
            name:'image',
            success(res){ 
              console.log(res)
              var str = res.data.split(',')
              let code = str[0].split(':')[1]
              if (code == 1) {
                let length = str[3].length - 3
                let url = str[3].substring(13,length)
                detailPicUrls.push(url)
                console.log(detailPicUrls)
                that.setData({
                  detailPicUrls
                })
              }else{
                app.errorMsg('上传图片失败')
              }
            },
            fail(res){
              console.log(res)
            }
          })
        }
      }
    })
  },
  //获取姓名
  bindGetName(e){
    this.setData({
      name:e.detail.value
    })
  },
  //获取手机号
  bindGetPhone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  getRemarks(e){
    this.setData({
      remarks:e.detail.value
    })
  },
  //提交
  voucherSubmit(){
    let that = this
    if (this.data.name=='') {
      app.errorMsg('请填写姓名')
    }else if (this.data.phone=='') {
      app.errorMsg('请填写手机号')
    }else{
      wx.showModal({
        title:'确认提交凭证信息',
        content:'确定提交后信息不可修改',
        confirmColor:'#539FFF',
        success(res){
          if (res.confirm) {
            let data = {
              id:that.data.orderId,
              buyerName:that.data.name,
              buyerPhone:that.data.phone,
              remarks:that.data.remarks,
              pic1:that.data.detailPicUrls[0]?that.data.detailPicUrls[0]:'',
              pic2:that.data.detailPicUrls[1]?that.data.detailPicUrls[1]:'',
              pic3:that.data.detailPicUrls[2]?that.data.detailPicUrls[2]:'',
              pic4:that.data.detailPicUrls[3]?that.data.detailPicUrls[3]:'',
              pic5:that.data.detailPicUrls[4]?that.data.detailPicUrls[4]:''
            }
            console.log(data)
          app.request('wx_app/user_order/voucher',data,'GET').then(res=>{
            console.log(res)
            if (res.code==1) {
              let pages = getCurrentPages()
              let prev = pages[pages.length - 2]
              prev.setData({
                voucher:true
              })
              wx.navigateBack({
                delta:1
              })
            }else{
              app.errorMsg(res.message)
            }
          })

        }else{
          console.log('用户点击取消')
        }
      }
    })
    }
    

  }
})